// position.js
const appConfig = require('../../config/config.js');
const http = require('../../utils/http.js');
const nav = require('../../utils/navigator.js');
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqMap;
//获取应用实例
const app = getApp();
Page({
  data: {
    codeMsg:'',
    currentAddress:'',
    latitude: 0, //地图初次加载时的纬度坐标
    longitude: 0, //地图初次加载时的经度坐标
    markers: [], //地图标点
    circles: [] //地图半径
  },
  onLoad: function() {
    var that = this;
    console.log(appConfig.QQMapWXKey)
    //获取图片信息
    var getUserPic = function(pic_url, i) {
      let cachePath;
      if (pic_url == null || pic_url == '') return;
      wx.downloadFile({
        url: pic_url,
        success: (pathInfo) => {
          // pathInfo.path 这是下载成的缓存链接，模拟器marker有时不支持http开头，真机不影响，得去掉http:/
          cachePath = pathInfo.tempFilePath.replace("http:/", '').replace("https:/", '') //真机中无需replace，都支持，
          var mak = "markers[" + i + "].iconPath";
          that.setData({
            [mak]: cachePath
          })
        }
      });

    }
    // 实例化API核心类
    qqMap = new QQMapWX({
      key: appConfig.QQMapWXKey
    });
    //获取地理位置
    app.getLocationInfo(function(data) {
      // 调用腾讯地图的接口查询当前位置
      qqMap.reverseGeocoder({
        location: {
          latitude: data.latitude,
          longitude: data.longitude
        },
        success: function(res1) {
          console.log(res1);
          
          http.post(appConfig.baseUrl + 'Notice/QueryRangeList', {
              PageIndex: 1,
              PageSize: 1000,
              Latitude: data.latitude,
              Longitude: data.longitude,
              Radius:30000
            },
            function(res) { //
              if (res.data) {
                console.log(res.data)
                console.log(res.data.Content)
                var marker = [];
                for (var i = 0; i < res.data.Content.length; i++) {
                  var pic = getUserPic(res.data.Content[i].ImgUrl, i);
                  marker[i] = {
                    id: res.data.Content[i].Id,
                    latitude: res.data.Content[i].Latitude,
                    longitude: res.data.Content[i].Longitude,
                    iconPath: '/images/marker.png',
                    width: 32,
                    height: 32
                  };

                }
                var circles = [{
                  latitude: data.latitude,
                  longitude: data.longitude,
                  //color: '#FF0000DD',
                  fillColor: '#7cb5ec88',
                  radius: 3000,
                  strokeWidth: 1
                }];
                that.setData({
                  circles: circles,
                  markers: marker,
                  latitude: data.latitude,
                  longitude: data.longitude,
                  codeMsg: res.data.CodeMsg,
                  currentAddress: res1.result.address_component.street
                });
              }
            },
            function(res) {
              console.log(res);
            });

        },
        fail: function(res) {
          console.log(res);
        },
        complete: function(res) {
          // console.log(res);
        }
      });
    })

  },
  bindMarkerClick:function(e){
    console.log(e);
    nav.navigateTo("../detail/detail?noticeId=" + e.markerId);
  }

});